package com.gmh.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gmh.dto.LoginFormDTO;
import com.gmh.dto.Result;
import com.gmh.dto.UserDTO;
import com.gmh.entity.User;
import com.gmh.entity.UserInfo;
import com.gmh.service.IUserInfoService;
import com.gmh.service.IUserService;
import com.gmh.utils.UserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IUserInfoService userInfoService;

    /**
     * 发送手机验证码
     */
    @PostMapping("code")
    @ApiOperation("发送手机验证码")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
        //发送短信验证码并保存验证码
        return userService.sendCode(phone, session);
    }

    /**
     * 登录功能
     * @param loginForm 登录参数，包含手机号、验证码；或者手机号、密码
     */
    @PostMapping("/login")
    @ApiOperation("登录功能")
    public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){
        // 实现登录功能
        return userService.login(loginForm, session);
    }

    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    @ApiOperation("登出功能")
    public Result logout(){
        return userService.logout();
    }

    @GetMapping("/me")
    @ApiOperation("获取当前登录用户")
    public Result me(){
        return userService.me();
    }

    @GetMapping("/info/{id}")
    @ApiOperation("查询用户详情")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询用户详情")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        return Result.ok(userDTO);
    }
    @PostMapping("/sign")
    @ApiOperation("签到功能")
    public Result sign(){
        //  签到功能
        return userService.sign();
    }

    @GetMapping("/sign/count")
    @ApiOperation("查询签到次数")
    public Result signCount(){
        //  签到次数
        return userService.signCount();
    }

    //用户密码登录
    @PostMapping("/login/password")
    @ApiOperation("用户密码登录")
    public Result loginPassword(@RequestBody LoginFormDTO loginForm, HttpSession session){
        return userService.loginPassword(loginForm, session);
    }
    //用户密码更改
    @PostMapping("/password/update")
    @ApiOperation("用户密码更改")
    public Result updatePassword(@RequestBody LoginFormDTO loginForm, HttpSession session){
        return userService.updatePassword(loginForm, session);
    }
}
